/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

//XFCE4 specific stuff

#include <gtk/gtk.h>

#include <gdk/gdkkeysyms.h>

#   include <X11/Xlib.h>
#   include <X11/Xutil.h>
#   include <X11/extensions/scrnsaver.h>
#   include <gdk/gdkx.h>

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <libxfce4panel/xfce-panel-plugin.h>

static void gelogger_construct (XfcePanelPlugin *plugin);

//register plugin
XFCE_PANEL_PLUGIN_REGISTER_INTERNAL(gelogger_construct);


	

static void gelogger_read_rc_file (XfcePanelPlugin *plugin, t_gelogger* gelogger )
{
    char *file;
    XfceRc *rc;
    
    gelogger->nologtime        = DEF_NOLOGTIME;
    gelogger->rep_absence_time = DEF_REP_ABSENCE_TIME;
    gelogger->logfile          = get_default_logfile();
    
    
    if ((file = xfce_panel_plugin_lookup_rc_file (plugin)) != NULL)
    {
        rc = xfce_rc_simple_open (file, TRUE);
        g_free (file);

        if (rc != NULL)
        {
			debug( 1, gelogger, "[debug] load data" );
            gelogger->nologtime        = xfce_rc_read_int_entry ( rc, "nologtime", DEF_NOLOGTIME );
            gelogger->rep_absence_time = xfce_rc_read_int_entry ( rc, "rep_absence_time", DEF_REP_ABSENCE_TIME );
            gelogger->logfile          = g_strdup( xfce_rc_read_entry ( rc, "logfile", get_default_logfile() ) );
            gelogger->debug            = xfce_rc_read_int_entry ( rc, "debug", 0 );
            xfce_rc_close (rc);
        }
    }
}


static void gelogger_write_rc_file (XfcePanelPlugin *plugin, t_gelogger *gelogger )
{
    char *file;
    XfceRc *rc;

	debug( 2, gelogger, "[debug] save data 0" );
    
    if (!(file = xfce_panel_plugin_save_location (plugin, TRUE)))
        return;

	debug( 2, gelogger, "[debug] save data 1" );

    rc = xfce_rc_simple_open (file, FALSE);
    g_free (file);

    if (!rc)
        return;

	debug( 1, gelogger, "[debug] save data 2" );

    xfce_rc_write_int_entry (rc, "nologtime", gelogger->nologtime);
    xfce_rc_write_int_entry (rc, "rep_absence_time", gelogger->rep_absence_time);
    xfce_rc_write_int_entry (rc, "debug", gelogger->debug);
    xfce_rc_write_entry (rc, "logfile", gelogger->logfile);
    
    xfce_rc_close (rc);
}

static void gelogger_free_data (XfcePanelPlugin *plugin, gpointer user_data)
{
	t_gelogger* gelogger = (t_gelogger*)user_data;
	
	debug( 1, gelogger, "[debug] free data" );
	
	gelogger_write_rc_file( plugin, gelogger );

	debug( 3, gelogger, "[debug] free data 1" );

	g_source_remove (gelogger->timeout_id);

	report( gelogger, time( NULL ), "[WLSTOP] shutdown plugin" );

	g_free( gelogger->logfile );

	debug( 3, gelogger, "[debug] free data 2" );

	g_free( gelogger );

	debug( 3, gelogger, "[debug] free data 3" );

}

static gboolean gain_focus( GtkEntry       *entry,
		 GdkEventButton    *event,
		 t_gelogger *gelogger )
{
	
	xfce_panel_plugin_focus_widget( gelogger->plugin, gelogger->editbox );
	
    return FALSE;
}

static gboolean command_key(GtkEntry   *entry,
		   GdkEventKey *event,
		   t_gelogger  *gelogger )
{

	guint key = event->keyval;
	
	if(key == GDK_Return
	    || key == GDK_KP_Enter
	    || key == GDK_ISO_Enter
	    || key == GDK_3270_Enter)
	{
		gelogger->nologcounter = 0;
		report( gelogger, time( NULL ), (char*)gtk_entry_get_text( entry  ) );

//		show_bottom_entries( gelogger );

		gtk_entry_set_text(entry, (gchar*)"" );
	}

	return FALSE;
}


static gboolean gelogger_set_size (XfcePanelPlugin *plugin, int size, t_gelogger* gelogger)
{
	gtk_widget_set_size_request (GTK_WIDGET (gelogger->plugin), -1, -1);
	gtk_widget_queue_resize (GTK_WIDGET (gelogger->plugin));

    return TRUE;
}


static void gelogger_dialog_response (GtkWidget *dlg, int reponse, t_gelogger *gelogger)
{
	char msg[1024];
	
	gchar* oldlog = gelogger->logfile;
	
	
	debug( 2, gelogger, "[debug] destroy dialog" );
	
	gelogger->nologtime = gtk_range_get_value( (GtkRange*)gelogger->pdlg.nologtime ) * 60;
	
	sprintf( msg, "[debug] setup nologtime = [%d]", gelogger->nologtime );
	
	debug( 2, gelogger, msg );
	
	gelogger->rep_absence_time = gtk_range_get_value( (GtkRange*)gelogger->pdlg.rep_absence_time ) * 60;
	
	sprintf( msg, "[debug] setup rep_absence_time = [%d]", gelogger->rep_absence_time );
	
	debug( 2, gelogger, msg );

	
	gelogger->logfile = g_strdup( gtk_entry_get_text( (GtkEntry*)gelogger->pdlg.logfile ) );
	
	snprintf( msg, 1023, "[debug] setup logfile = [%s]", gelogger->logfile );
	
	debug( 2, gelogger, msg );
	
	if( strcmp( oldlog, gelogger->logfile ) )
	{
		report( gelogger, gelogger->work_start, "[WLSTART] start using new logfile" );
	}
	
	
	gelogger->debug = gtk_range_get_value( (GtkRange*)gelogger->pdlg.debug );
	
	sprintf( msg, "[debug] setup debug = [%d]", gelogger->debug );
	
	debug( 2, gelogger, msg );

    g_object_set_data (G_OBJECT (gelogger->plugin), "dialog", NULL);

	debug( 2, gelogger, "[debug] destroy dialog" );
	
	g_free( oldlog );
		
    gtk_widget_destroy (dlg);
    xfce_panel_plugin_unblock_menu (gelogger->plugin);
    gelogger_write_rc_file (gelogger->plugin, gelogger);
}

static void gelogger_properties_dialog (XfcePanelPlugin *plugin, t_gelogger* gelogger )
{
    GtkWidget *dlg, *frame, *bin, *vbox, *ctl, *hbox;
    int pos;

    xfce_panel_plugin_block_menu (plugin);
    
 
    dlg = xfce_titled_dialog_new_with_buttons (_("gelogger"), NULL,
                GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
                GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
                NULL);

    gtk_window_set_screen (GTK_WINDOW (dlg), 
                           gtk_widget_get_screen (GTK_WIDGET (plugin)));

    g_object_set_data (G_OBJECT (plugin), "dialog", dlg);

    gtk_window_set_position (GTK_WINDOW (dlg), GTK_WIN_POS_CENTER);
    gtk_window_set_icon_name (GTK_WINDOW (dlg), "xfce4-settings");
    
    gtk_container_set_border_width (GTK_CONTAINER (dlg), 2);
    

	//CONTENT
	
//	GtkWidget*	nologtime; 
	
	
    frame = xfce_create_framebox (_("Log every (min)"), &bin);
    gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame,
                        FALSE, FALSE, 0);
    
    vbox = gtk_vbox_new (FALSE, 8);
    gtk_container_add (GTK_CONTAINER (bin), vbox);

	ctl = gelogger->pdlg.nologtime = gtk_hscale_new_with_range( 1, 60, 5 );
	
	gtk_box_pack_start (GTK_BOX (vbox), ctl, FALSE, FALSE, 0);
	
	gtk_range_set_value( (GtkRange*)ctl, gelogger->nologtime / 60 );
	
//	GtkWidget*	rep_absence_time;

    frame = xfce_create_framebox (_("Report absence if away (min)"), &bin);
    gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame,
                        FALSE, FALSE, 0);
    
    vbox = gtk_vbox_new (FALSE, 8);
    gtk_container_add (GTK_CONTAINER (bin), vbox);

	ctl = gelogger->pdlg.rep_absence_time = gtk_hscale_new_with_range( 1, 60, 15 );
	
	gtk_box_pack_start (GTK_BOX (vbox), ctl, FALSE, FALSE, 0);
	
	gtk_range_set_value( (GtkRange*)ctl, gelogger->rep_absence_time / 60 );


//	GtkWidget*	logfile;

    frame = xfce_create_framebox (_("Logfile"), &bin);
    gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame,
                        FALSE, FALSE, 0);
    
    vbox = gtk_vbox_new (FALSE, 8);
    gtk_container_add (GTK_CONTAINER (bin), vbox);

	ctl = gelogger->pdlg.logfile = gtk_entry_new();

	gtk_box_pack_start (GTK_BOX (vbox), ctl, FALSE, FALSE, 0);

	gtk_entry_set_text( (GtkEntry*)ctl, gelogger->logfile );

//	GtkWidget*	debug;	
	
    frame = xfce_create_framebox (_("Debuglevel"), &bin);
    gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame,
                        FALSE, FALSE, 0);
    
    vbox = gtk_vbox_new (FALSE, 8);
    gtk_container_add (GTK_CONTAINER (bin), vbox);

	ctl = gelogger->pdlg.debug = gtk_hscale_new_with_range( 0, 8, 1 );
	
	gtk_box_pack_start (GTK_BOX (vbox), ctl, FALSE, FALSE, 0);
	
	gtk_range_set_value( (GtkRange*)ctl, gelogger->debug );
	
	
	
    g_signal_connect (dlg, "response",
            G_CALLBACK (gelogger_dialog_response), gelogger);

    gtk_widget_show_all (dlg);
}


//constructor function, called while plugin loading
static void gelogger_construct ( XfcePanelPlugin *plugin )
{
	t_gelogger* gelogger = g_new0( t_gelogger, 1 );
	//load config, eventually setup with default values
	gelogger_read_rc_file( plugin, gelogger );

//	gelogger->logfile = "/tmp/dbg_gelogger.log";
//	gelogger->debug = 1;

	gelogger->absence_start = time( NULL );

	gelogger->plugin = plugin;
	
	gelogger->frame = gtk_frame_new( NULL );
	
	gelogger->hbox = gtk_hbox_new (FALSE, 0);
	gelogger->icon = gtk_image_new_from_stock( GTK_STOCK_ABOUT, GTK_ICON_SIZE_SMALL_TOOLBAR );
			
	g_object_ref( (gpointer)gelogger->icon );
	
	gelogger->editbox = gtk_entry_new();

	gtk_entry_set_editable( GTK_ENTRY(gelogger->editbox), TRUE );

	g_signal_connect( gelogger->editbox, "button_press_event",
		      G_CALLBACK (gain_focus), gelogger );

	g_signal_connect ( gelogger->editbox, "key_press_event",
		      G_CALLBACK (command_key ), gelogger );

    g_signal_connect (plugin, "free-data", 
                      G_CALLBACK (gelogger_free_data), gelogger);
    
    g_signal_connect (plugin, "save", 
                      G_CALLBACK (gelogger_write_rc_file), gelogger);
                      
    g_signal_connect (plugin, "size-changed", 
                      G_CALLBACK (gelogger_set_size), gelogger);                      

    xfce_panel_plugin_menu_show_configure (plugin);
    g_signal_connect (plugin, "configure-plugin", 
                      G_CALLBACK (gelogger_properties_dialog), gelogger);

	
	gtk_box_pack_start (GTK_BOX (gelogger->hbox), gelogger->icon, FALSE, FALSE, 5);
	gtk_box_pack_end (GTK_BOX (gelogger->hbox), gelogger->editbox, FALSE, FALSE, 5);

	gtk_window_set_resizable( (GtkWindow*)gelogger->frame, TRUE );
//	gtk_widget_set_size_request (GTK_WIDGET (gelogger->frame), -1, -1);
//	gtk_widget_queue_resize (GTK_WIDGET (gelogger->frame));
	
	gtk_window_set_resizable( (GtkWindow*)gelogger->hbox, FALSE );

	gtk_container_add (GTK_CONTAINER (gelogger->frame), gelogger->hbox);

	gtk_container_add (GTK_CONTAINER (plugin), gelogger->frame);

	gtk_widget_set_size_request (GTK_WIDGET (gelogger->plugin), -1, -1);
	gtk_widget_queue_resize (GTK_WIDGET (gelogger->plugin));

	gtk_widget_show_all( GTK_WIDGET (plugin) );


//	xfce_panel_plugin_set_expand( plugin, TRUE );

	//check absense every IDLE_SPAN seconds	
	gelogger->timeout_id = g_timeout_add( 1000 * IDLE_SPAN, check_absence, gelogger );
	
	//log plugin startup event
	report( gelogger, time( NULL ), "[WLSTART] plugin started" );
}
